package org.mbds.android.p2p.receivers;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.mbds.android.p2p.services.P2pService;
import org.mbds.android.p2p.threads.SocketConnexion;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.widget.Toast;

public class P2pBroadcastReceiver extends BroadcastReceiver{
	
	P2pService mService;
	WifiP2pManager mWifiManager;
	Channel mChannel;
	boolean mWifiEnabled;
	
	/**
	 * 
	 * @param manager : the wifi P2p manager.
	 * @param channel 
	 * @param service : the service that'll be notified by the broadcast events.
	 * 
	 */
	public P2pBroadcastReceiver(WifiP2pManager manager, Channel channel, P2pService service){
		super();
		mWifiManager = manager;
		mChannel = channel;
		mService = service;
		mWifiEnabled = false;
	}
	
	//Method that receives P2p wifi notifications. Get the action sent by the intent and perform appropriate operations.
	@SuppressLint("InlinedApi")
	@Override
	public void onReceive(Context context, Intent intent) {
		
		String action = intent.getAction();
		
		if(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)){ // Broadcasted when the Wifi state changed ; enabled or disabled.
			
			int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); // Get the state of the wifi.
			switch(state){
				case WifiP2pManager.WIFI_P2P_STATE_ENABLED : 
					mWifiEnabled = true;
					mService.setP2pWifiState(true);
					break;
				case WifiP2pManager.WIFI_P2P_STATE_DISABLED :
					mWifiEnabled = false;
					mService.setP2pWifiState(false);
					break;
				default :
					mWifiEnabled = false;
					mService.setP2pWifiState(false);
					break;
			}
			
			Toast.makeText(context, "WIFI_P2P_STATE_CHANGED_ACTION", Toast.LENGTH_SHORT).show();
		}else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){
			if(mWifiManager == null)
				return;
			
			//Get the network informations.
			NetworkInfo netWorkInfos = (NetworkInfo)intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
			Toast.makeText(context, "NET WORKS", Toast.LENGTH_SHORT).show();
			if(netWorkInfos.isConnected()){
				mWifiManager.requestConnectionInfo(mChannel, new WifiP2pManager.ConnectionInfoListener() {
					
					@Override
					public void onConnectionInfoAvailable(WifiP2pInfo info) {
						mService.addWifiP2pInfo(info);
					}
				});
			}
			
		}else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){ // Broadcasted when call is made to discoveryPeers()
			Toast.makeText(context, "WIFI_P2P_PEERS_CHANGED_ACTION", Toast.LENGTH_SHORT).show();
		}else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){ // Broadcasted when the current device informations changed.
			Toast.makeText(context, "WIFI_P2P_THIS_DEVICE_CHANGED_ACTION", Toast.LENGTH_SHORT).show();
		}else{ 
			
		}
		
	}
	
	public boolean getWifiState(){
		return mWifiEnabled;
	}
}
